package doublepointer;


import java.util.HashMap;
import java.util.Map;

/**
 * @author 阿帕奇
 * @date 2021/5/13 9:59 下午
 */

public class SlidingWindowAlgorithm {
    public static String test(String source, String target) {
        int left=0, right = 0;
        Map<Character, Integer> window = new HashMap<>();
        Map<Character, Integer> needs = new HashMap<>();
        for (char c : target.toCharArray()) {
            needs.put(c, needs.getOrDefault(c, 0) + 1);
        }
        String resp = source;


        while (right < source.length() - 1) {
            char c = source.charAt(right);
            window.put(c, window.getOrDefault(c, 0) + 1);

            right++;
            while (left < right - 1 && needs.keySet().stream().noneMatch(k -> needs.get(k) > window.getOrDefault(k, 0))) {
                char cc = source.charAt(left);
                resp = source.substring(left, right);
                window.put(cc, window.getOrDefault(cc, 1) - 1);
                left++;
            }
            if (needs.keySet().stream().noneMatch(k -> needs.get(k) > window.getOrDefault(k, 0))) {
                resp = source.substring(left - 1, right);
            }
        }
        System.out.println(resp);
        return resp;
    }


}